﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using WuDada.Core.SystemApplications.Domain;
using WuDada.Core.Member.Domain;
using WuDada.Core.Member.Service;
using WuDada.Core.Member;

public partial class admin_UC02_0204_1 : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private MemberFactory m_MemberFactory;
    private IPostService m_PostService;
    private ISignUpService m_SignUpService;
    private IMemberService m_MemberService;
    private WebLogService m_WebLogService;

    //傳入的父層Id
    private int m_ParentPostId
    {
        get { if (ViewState["parent"] == null) { ViewState["parent"] = 0; } return int.Parse(ViewState["parent"].ToString()); }
        set { ViewState["parent"] = value; }
    }
    private string m_ParentPath = "0204.aspx";

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_MemberFactory = new MemberFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_SignUpService = m_PostFactory.GetSignUpService();
        m_MemberService = m_MemberFactory.GetMemberService();
        m_WebLogService = new WebLogService();

        if (!Page.IsPostBack)
        {
            m_ParentPostId = QueryStringHelper.GetInteger("parent", 0);
            PostVO parentPost = m_PostService.GetPostById(m_ParentPostId);
            if (parentPost == null)
            {
                string strJs = JavascriptUtil.AlertJSAndRedirect("查無父層分類資訊", m_ParentPath);
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", strJs, false);
            }

            lblParentTitle.Text = parentPost.Title;
            pnlContent.Visible = false;
            fillGridView();
            //ShowMode();
        }
    }

    //private void ShowMode()
    //{
    //    if (m_Mode == 0)
    //    {
    //        btnSave.Visible = false;
    //    }
    //    else
    //    {
    //        btnSave.Visible = true;
    //    }
    //}

    protected void btnQuery_Click(object sender, ImageClickEventArgs e)
    {
        fillGridView();
    }

    private void fillGridView()
    {
        StringBuilder sbHql = new StringBuilder();
        sbHql.Append(string.Format("WHERE Post.PostId = {0} ", m_ParentPostId));

        //搜尋條件
        string name = txtSearchName.Text.Trim();
        if (!string.IsNullOrEmpty(name))
        {
            sbHql.Append(string.Format(@" AND Name like '%{0}%' ", name));
        }

        string dateFrom = txtDateFrom.Text.Trim();
        string dateTo = txtDateTo.Text.Trim();
        DateTime? dtFrom = ConvertUtil.ToDateTimeMin(dateFrom);
        DateTime? dtTo = ConvertUtil.ToDateTimeMax(dateTo);
        if (dtFrom != null && dtTo != null)
        {
            dtTo = dtTo.Value.AddDays(1);
            sbHql.Append(string.Format(@" AND CreateTime <= '{0}' AND CreateTime >= '{1}' "
                , dtTo.Value.ToShortDateString(), dtFrom.Value.ToShortDateString()));
        }
        else if (dtFrom != null)
        {
            sbHql.Append(string.Format(@" AND CreateTime >= '{0}' ", dtFrom.Value.ToShortDateString()));
        }
        else if (dtTo != null)
        {
            dtTo = dtTo.Value.AddDays(1);
            sbHql.Append(string.Format(@" AND CreateTime <= '{0}' ", dtTo.Value.ToShortDateString()));
        }

        //分頁
        AspNetPager1.RecordCount = m_SignUpService.CountSignUpByWhere(sbHql.ToString());
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        sbHql.Append(" ORDER BY CreateTime Desc ");
        gvDetail.DataSource = m_SignUpService.SearchSignUpByWhere(sbHql.ToString(), pageIndex, pageSize);
        gvDetail.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int signUpId = int.Parse(e.CommandArgument.ToString());
        SignUpVO signUpVO = m_SignUpService.GetSignUpById(signUpId);
        switch (cmdName)
        {
            case "myModify":
                ClearUI();
                m_Mode = signUpId;
                UIHelper.FillUI(pnlContent, signUpVO);
                if (signUpVO.Member != null)
                {
                    MemberVO memberVO = m_MemberService.GetMemberById(signUpVO.Member.MemberId);
                    txtLoginId.Text = memberVO.LoginId;
                }
                //ShowMode();
                pnlContent.Visible = true;
                break;
            default:
                break;
        }
        fillGridView();
    }


    protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            Control ctrl = e.Row;
            SignUpVO signUpVO = (SignUpVO)e.Row.DataItem;
            if (signUpVO.Member != null)
            {
                UIHelper.SetLabelText(ref ctrl, "lblIsMember", "V");
            }
            if (signUpVO.BirthDay != null)
            {
                UIHelper.SetLabelText(ref ctrl, "lblBirthDay", signUpVO.BirthDay.Value.ToString("yyyy/MM/dd"));
            }
        }
    }

    private void ClearUI()
    {
        m_Mode = 0;
        UIHelper.ClearUI(pnlContent);
        pnlContent.Visible = false;
    }

    //protected void btnSave_Click(object sender, EventArgs e)
    //{
    //    SignUpVO signUpVO = m_SignUpService.GetSignUpById(m_Mode);
    //    UIHelper.FillVO(pnlContent, signUpVO);
    //    m_SignUpService.UpdateSignUp(signUpVO);
    //    m_WebLogService.AddSystemLog(MsgVO.Action.修改, signUpVO);
    //    fillGridView();
    //    ClearUI();
    //    ShowMode();
    //}

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        //ShowMode();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(m_ParentPath);
    }
}